package actions.saturnMusic;

import actions.saturnMusic.dialog.ActivityTemplateDialog;
import bean.ClassConfigBean;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.impl.file.PsiDirectoryImpl;
import generator.ClassGenerator;
import org.jetbrains.annotations.NotNull;
import utils.CommonUtil;

import java.util.HashMap;
import java.util.Map;

public class GenerateSaturnMVPGroup extends AnAction {

    @Override
    public void actionPerformed(@NotNull AnActionEvent event) {
        ActivityTemplateDialog.runWithCallBack((input, layout, type) -> {
            if (CommonUtil.isEmpty(input)) {
                CommonUtil.showError("请正确输入");
                return;
            }

            PsiDirectory parent = initParent(event, input);
            if (parent == null) {
                return;
            }
            String parentPath = parent.getVirtualFile().getPath();
            String[] parentParts = parentPath.split("/");
            String baseName = CommonUtil.upperStringFirst(parentParts[parentParts.length - 1]);
            String packStr = CommonUtil.getAndroidPackageName(event);
            String filePackage = parentPath.substring(parentPath.indexOf(packStr.replace('.', '/'))).replace('/', '.');

            ClassConfigBean[] configBeans = {
                    new ClassConfigBean("MVPView", baseName + type.getName() + ".java"),
                    new ClassConfigBean("MVPPresenter", baseName + "Presenter.java"),
                    new ClassConfigBean("MVPModel", baseName + "Model.java"),
                    new ClassConfigBean("MVPServices", baseName + "Services.java"),
                    new ClassConfigBean("MVPBeans", baseName + "Beans.java"),
            };


            Map<String, String> fileKeyMap = new HashMap<>();
            fileKeyMap.put("$base", baseName);
            fileKeyMap.put("$filePackage", filePackage);
            fileKeyMap.put("$layoutId", layout);
            fileKeyMap.put("$type", type.getName());

            if (!ClassGenerator.generate(parent, fileKeyMap, configBeans)) {
                CommonUtil.showMessage("Failed!", "文件生成失败！");
            } else {
                CommonUtil.showMessage("Success!", "文件组生成成功！");
                parent.getVirtualFile().refresh(false, true);
            }
        });
    }

    private PsiDirectory initParent(AnActionEvent event, String parentName) {
        PsiDirectoryImpl nowSelect = CommonUtil.getNowSelect(event);
        if (nowSelect == null) {
            return null;
        }
        PsiDirectory parent = nowSelect.findSubdirectory(parentName);
        if (parent != null) {
            int confirmRst = CommonUtil.requestConfirm("已存在文件，删除以继续？");
            if (confirmRst == 0) {
                parent.delete();
            } else {
                return null;
            }
        }
        return nowSelect.createSubdirectory(parentName);
    }
}
